package main

import (
	"fmt"
	"sort"
	"strings"
)

func main() {
	// make函数创建map类型的数据
	//var userinfo = make(map[string]string)
	//userinfo["name"] = "张三"
	//userinfo["age"] = "18"
	//userinfo["sex"] = "男"
	//fmt.Println(userinfo)

	// 声明式创建map
	//userinfo := map[string]string{
	//	"name": "张三",
	//	"age":  "18",
	//	"sex":  "男",
	//}
	//// 修改名称
	//userinfo["name"] = "李四"
	//fmt.Println(userinfo)
	//for k, v := range userinfo {
	//	fmt.Printf("%s:%s\n", k, v)
	//}
	//
	//fmt.Println(userinfo["sex"])
	//fmt.Println("------------")
	//value, contains := userinfo["age"]
	//fmt.Println(value, contains)
	//
	//// 删除age
	//delete(userinfo, "age")
	//fmt.Println(userinfo)

	// 在切片中放一些列的用户信息
	//var slice = make([]map[string]string, 3, 3)
	//slice[0] = make(map[string]string)
	//slice[0]["name"] = "张三"
	//slice[0]["age"] = "18"
	//slice[0]["sex"] = "男"
	//fmt.Println(slice)

	//var slice = make([]map[string][]string, 3, 3)
	//slice[0] = make(map[string][]string)
	//slice[0]["name"] = []string{"张三", "李四"}
	//fmt.Println(slice)

	//map的排序
	map1 := make(map[int]int, 10)
	map1[10] = 100
	map1[1] = 13
	map1[4] = 56
	map1[8] = 90
	map1[11] = 100
	map1[2] = 13

	// fmt.Println(map1)

	/*
		1 13
		4 56
		10 100
		8 90
	*/
	// for key, val := range map1 {
	// 	fmt.Println(key, val)
	// }

	//需求：按照key升序输出map的key=>value   (签名算法)
	//1、把map的key放在切片里面
	var keySlice []int
	for key := range map1 {
		keySlice = append(keySlice, key)
	}
	fmt.Println(keySlice) //[11 10 4 8 2 1]

	//2、让key进行升序排序
	sort.Ints(keySlice)
	fmt.Println(keySlice) //[1 2 4 8 10 11]

	//3、循环遍历key输出map的值
	for _, v := range keySlice {
		fmt.Printf("key=%v value=%v\n", v, map1[v])
	}

	// 统计字符串中每个单词出现的次数
	var wordMap = make(map[string]int)
	var str = "how do you do"
	var arrSlice = strings.Split(str, " ")
	for _, word := range arrSlice {
		wordMap[word]++
	}
	fmt.Println(wordMap)
}
